#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Word {
  string s;
  int no;
};

vector<Word> dict;

bool operator<(const Word& lhs, const Word& rhs) {
  return lhs.s < rhs.s;
}

bool IsPrefix(const string& s, const string& pre) {
  if (pre.size() > s.size()) {
    return false;
  }
  for (int i = 0; i < pre.size(); i++) {
    if (pre[i] != s[i]) {
      return false;
    }
  }
  return true;
}

int main() {
  freopen("auto.in", "r", stdin);
  freopen("auto.out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int w, n;

  cin >> w >> n;
  dict.resize(w);
  for (int i = 0; i < w; i++) {
    cin >> dict[i].s;
    dict[i].no = i + 1;
  }
  sort(dict.begin(), dict.end());

  for (int i = 0; i < n; i++) {
    int k_i;
    string pre;

    cin >> k_i >> pre;

    int ans = -1;

    int id = lower_bound(dict.begin(), dict.end(), Word{pre, -1}) - dict.begin();

    if (id + k_i - 1 < w && IsPrefix(dict[id + k_i - 1].s, pre)) {
      ans = dict[id + k_i - 1].no;
    }
    cout << ans << "\n";
  }
  return 0;
}